const express = require('express');
const router = express.Router();
const db = require('../../utils/db');

// 获取商家信息
router.get('/getshop',function(req,res){
    let mysql='SELECT * FROM shop_information';
    db(mysql,null,(err,data)=>{
        if (err) {
            return res.json({
              code: 500,
              msg: '查询店铺信息失败'
            })
          }
        res.json({
            code: 200,
            msg: '查询店铺信息成功',
            data: data
        })
    })
})

// 获取登录用户信息
router.get('/getuser',function(req,res){
    let mysql='SELECT * FROM customer JOIN address ON customer.customer_id=address.address_customer_id WHERE customer_id=? AND address_type=1';
    db(mysql,[req.query.id],(err,data)=>{
        if (err) {
            return res.json({
              code: 500,
              msg: '查询用户信息失败'
            })
          }
        res.json({
            code: 200,
            msg: '查询用户信息成功',
            data: data
        })
    })
})

// 获取折扣商品数据
router.get('/getdiscountfood',function(req,res){
    let mysql='SELECT menu_id,pack_discount,menu_price FROM package_list JOIN menu_file ON package_list.pack_num=menu_file.menu_id';
    db(mysql,null,(err,data)=>{
        if (err) {
            return res.json({
              code: 500,
              msg: '查询折扣商品失败'
            })
          }
          res.json({
            code: 200,
            msg: '查询折扣商品成功',
            data: data
          })
    })
})

// 获取菜品月销售量
router.get('/getmonthsale',function(req,res){
    let mysql=`SELECT menu_id,dish_amount,order_time FROM dish_flowing 
               JOIN menu_file ON dish_flowing.dish_name=menu_file.menu_id
               JOIN order_information ON dish_flowing.dish_time=order_information.order_id 
               WHERE menu_id=${req.query.id} AND order_time LIKE '${req.query.time}%'`;
    db(mysql,null,(err,data)=>{
        if (err) {
            return res.json({
              code: 500,
              msg: '查询商品月销售量失败'
            })
          }
          res.json({
            code: 200,
            msg: '查询商品月销售量成功',
            data: data
          })
    })
})

// 获取最大订单编号和订单支付编号
router.get('/getnum',function(req,res){
  let mysql=`SELECT order_id,order_num,order_payfor_num FROM order_information GROUP BY order_num DESC LIMIT 0,1`;
  db(mysql,null,(err,data)=>{
      if (err) {
          return res.json({
            code: 500,
            msg: '查询最大编号失败'
          })
        }
        res.json({
          code: 200,
          msg: '查询最大编号成功',
          data: data
        })
  })
})

// 获取用户领取的优惠券
router.get('/getusercoupon',function(req,res){
    let mysql=`SELECT * FROM coupons_receive JOIN customer ON customer.customer_id=coupons_receive.coup_customer_id 
               JOIN discounts_management ON coupons_receive.coup_dis_id=discounts_management.dis_id 
               WHERE coup_customer_id=?`;
    db(mysql,[req.query.id],(err,data)=>{
        if (err) {
            return res.json({
              code: 500,
              msg: '查询用户优惠券失败'
            })
          }
          res.json({
            code: 200,
            msg: '查询用户优惠券成功',
            data: data
          })
    })
})

// 获取店铺优惠券
router.get('/getcoupon',function(req,res){
    let mysql=`SELECT * FROM discounts_management WHERE dis_effctive_state='未过期' AND dis_publish_state='未生效'`;
    db(mysql,null,(err,data)=>{
        if (err) {
            return res.json({
              code: 500,
              msg: '查询店铺优惠券失败'
            })
          }
          res.json({
            code: 200,
            msg: '查询店铺优惠券成功',
            data: data
          })
    })
})

// 添加订单
router.post('/addorder',function(req,res){
  let mysql=`INSERT INTO order_information VALUES
  (NULL,?,'正常',?,1,?,?,DEFAULT,DEFAULT,NULL,NULL,NULL,?,?,?,?,?,?,?,?,?,?,?,?,DEFAULT)`;
  db(mysql,[req.body.ordernum,req.body.userid,req.body.orderpaynum,req.body.paystate,
            req.body.addressid,req.body.deliverytime,req.body.packing,req.body.sendfee,
            req.body.fullfee,req.body.couponfee,req.body.msg,req.body.ordertime,
            req.body.placeordertime,req.body.paytime,req.body.totalprice,req.body.realprice],(err,data)=>{
      if (err) {
        return res.json({
          code: 500,
          msg: '添加订单失败'
        })
      }
      res.json({
        code: 200,
        msg: '添加订单成功'
      })
  })
})

// 添加订单菜品
router.post('/addorderfood',function(req,res){
  let mysql=`INSERT INTO orderfood VALUES(NULL,?,?,?)`;
  db(mysql,[req.body.orderid,req.body.menuid,req.body.num],(err,data)=>{
      if (err) {
        return res.json({
          code: 500,
          msg: '添加订单菜品失败'
        })
      }
      res.json({
        code: 200,
        msg: '添加订单菜品成功'
      })
  })
})

// 修改用户优惠券使用状态
router.post('/updatecoupon',function(req,res){
  let mysql=`UPDATE SET coupons_receive coup_status='使用' WHERE coup_customer_id=?`;
  db(mysql,[req.body.couponid],(err,data)=>{
      if (err) {
        return res.json({
          code: 500,
          msg: '修改用户优惠券状态失败'
        })
      }
      res.json({
        code: 200,
        msg: '修改用户优惠券状态成功'
      })
  })
})

module.exports = router;